package cn.e4j.modular.crm.controller.base;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwadT;
import cn.e4j.modular.crm.entity.GwadlT;
import cn.e4j.modular.crm.service.impl.Gwai220ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 区域别控制类
 *
 * @author yuan_rz
 * @Date 2019/01/11 17:11
 */
@Controller
@RequestMapping("/base/gwai220")
public class Gwai220Controller extends BaseController {

    @Autowired
    private Gwai220ServiceImpl gwai220Service;

    /**
     * 跳转页面
     */
    @RequiresPermissions("base:gwai220:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/base/gwai220.html";
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("base:gwai220:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwai220Service.getListData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 地区开窗
     */
    @RequestMapping("/getAreaList")
    @ResponseBody
    public ResponseData getAreaList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwai220Service.getAreaList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value="/exportListData",method=RequestMethod.GET)
    @RequiresPermissions("base:gwai220:init")
    public void exportListData(ModelMap map, HttpServletRequest request, HttpServletResponse response){

    }

    /**
     * 添加数据
     */
    @RequestMapping("/addData")
    @RequiresPermissions("base:gwai220:init")
    @ResponseBody
    public ResponseData addData(GwadT gwadT, GwadlT gwadlT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwadT.setGWADENT(shiroUser.getDBC());
        gwadlT.setGWADLENT(shiroUser.getDBC());
        gwadlT.setGWADL001(gwadT.getGWAD001());
        gwadlT.setGWADL002(gwadT.getGWAD002());
        gwadlT.setGWADL003(LangUtils.getLang(shiroUser.getLANG()));
        if(gwai220Service.existedTheParams(gwadT)){
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode() ,
                    BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
        }
        gwai220Service.addData(gwadT,gwadlT);
        return SUCCESS_TIP;
    }

    /**
     * 修改数据
     */
    @RequestMapping("/editData")
    @RequiresPermissions("base:gwai220:init")
    @ResponseBody
    public ResponseData editData(GwadT gwadT, GwadlT gwadlT,@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        GwadT oldGwadT = new GwadT();
        oldGwadT.setGWADENT(gwadT.getGWADENT());
        oldGwadT.setGWAD001(inDto.getString("oldGWAD001"));
        oldGwadT.setGWAD002(inDto.getString("oldGWAD002"));
        if(!(gwadT.getGWAD001().equals(oldGwadT.getGWAD001())&&
                gwadT.getGWAD002().equals(oldGwadT.getGWAD002()))){
            if(gwai220Service.existedTheParams(gwadT)){
                return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode() ,
                        BizExceptionEnum.EXISTED_THE_PARAMS.getMessage());
            }
        }
        gwai220Service.editData(gwadT,gwadlT,oldGwadT);
        return SUCCESS_TIP;
    }

    /**
     * 删除数据
     */
    @RequestMapping("/deleteData")
    @RequiresPermissions("base:gwai220:init")
    @ResponseBody
    public ResponseData deleteData(GwadT gwadT, GwadlT gwadlT) {
        gwai220Service.deleteData(gwadT,gwadlT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("base:gwai220:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwadT> gwadTList = inDto.getReqList(GwadT.class);
        List<GwadlT> gwadlTList = inDto.getReqList(GwadlT.class);
        gwai220Service.deleteDataBatch(gwadTList,gwadlTList);
        return SUCCESS_TIP;
    }

}
